home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / File64.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  4.1 KB  |  164 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <windows.h>
  19.  
  20. #include "Error.h"
  21.  
  22. #include "File64.h"
  23.  
  24. // hack...
  25.  
  26. extern CRITICAL_SECTION g_diskcs;
  27.  
  28. ////////////
  29.  
  30. File64::File64() {
  31. }
  32.  
  33. File64::File64(HANDLE _hFile, HANDLE _hFileUnbuffered)
  34. : hFile(_hFile), hFileUnbuffered(_hFileUnbuffered)
  35. {
  36.     i64FilePosition = 0;
  37. }
  38.  
  39. long File64::_readFile(void *data, long len) {
  40.     DWORD dwActual;
  41.  
  42.     if (!ReadFile(hFile, data, len, &dwActual, NULL))
  43.         return -1;
  44.  
  45.     i64FilePosition += dwActual;
  46.  
  47.     return (long)dwActual;
  48. }
  49.  
  50. void File64::_readFile2(void *data, long len) {
  51.     long lActual = _readFile(data, len);
  52.  
  53.     if (lActual < 0)
  54.         throw MyWin32Error("Failure reading file: %%s.",GetLastError());
  55.  
  56.     if (lActual != len)
  57.         throw MyError("Failure reading file: Unexpected end of file");
  58. }
  59.  
  60. bool File64::_readChunkHeader(FOURCC& pfcc, DWORD& pdwLen) {
  61.     DWORD dw[2];
  62.     long actual;
  63.  
  64.     actual = _readFile(dw, 8);
  65.  
  66.     if (actual != 8)
  67.         return false;
  68.  
  69.     pfcc = dw[0];
  70.     pdwLen = dw[1];
  71.  
  72.     return true;
  73. }
  74.  
  75. void File64::_seekFile(__int64 i64NewPos) {
  76.     LONG lHi = (LONG)(i64NewPos>>32);
  77.     DWORD dwError;
  78.  
  79.     if (0xFFFFFFFF == SetFilePointer(hFile, (LONG)i64NewPos, &lHi, FILE_BEGIN))
  80.         if ((dwError = GetLastError()) != NO_ERROR)
  81.             throw MyWin32Error("File64: %%s", dwError);
  82.  
  83.     i64FilePosition = i64NewPos;
  84. }
  85.  
  86. bool File64::_seekFile2(__int64 i64NewPos) {
  87.     LONG lHi = (LONG)(i64NewPos>>32);
  88.     DWORD dwError;
  89.  
  90. //    _RPT1(0,"Seeking to %I64d\n", i64NewPos);
  91.  
  92.     if (0xFFFFFFFF == SetFilePointer(hFile, (LONG)i64NewPos, &lHi, FILE_BEGIN))
  93.         if ((dwError = GetLastError()) != NO_ERROR)
  94.             return false;
  95.  
  96.     i64FilePosition = i64NewPos;
  97.  
  98.     return true;
  99. }
  100.  
  101. void File64::_skipFile(__int64 bytes) {
  102.     LONG lHi = (LONG)(bytes>>32);
  103.     DWORD dwError;
  104.     LONG lNewLow;
  105.  
  106.     if (0xFFFFFFFF == (lNewLow = SetFilePointer(hFile, (LONG)bytes, &lHi, FILE_CURRENT)))
  107.         if ((dwError = GetLastError()) != NO_ERROR)
  108.             throw MyWin32Error("File64: %%s", dwError);
  109.  
  110.     i64FilePosition = (unsigned long)lNewLow | (((__int64)(unsigned long)lHi)<<32);
  111. }
  112.  
  113. bool File64::_skipFile2(__int64 bytes) {
  114.     LONG lHi = (LONG)(bytes>>32);
  115.     DWORD dwError;
  116.     LONG lNewLow;
  117.  
  118.     if (0xFFFFFFFF == (lNewLow = SetFilePointer(hFile, (LONG)bytes, &lHi, FILE_CURRENT)))
  119.         if ((dwError = GetLastError()) != NO_ERROR)
  120.             return false;
  121.  
  122.     i64FilePosition = (unsigned long)lNewLow | (((__int64)(unsigned long)lHi)<<32);
  123.  
  124.     return true;
  125. }
  126.  
  127. long File64::_readFileUnbuffered(void *data, long len) {
  128.     DWORD dwActual;
  129.  
  130.     EnterCriticalSection(&g_diskcs);
  131.     if (!ReadFile(hFileUnbuffered, data, len, &dwActual, NULL)) {
  132.         LeaveCriticalSection(&g_diskcs);
  133.         return -1;
  134.     }
  135.     LeaveCriticalSection(&g_diskcs);
  136.  
  137.     return (long)dwActual;
  138. }
  139.  
  140. void File64::_seekFileUnbuffered(__int64 i64NewPos) {
  141.     LONG lHi = (LONG)(i64NewPos>>32);
  142.     DWORD dwError;
  143.  
  144.     if (0xFFFFFFFF == SetFilePointer(hFileUnbuffered, (LONG)i64NewPos, &lHi, FILE_BEGIN))
  145.         if ((dwError = GetLastError()) != NO_ERROR)
  146.             throw MyWin32Error("File64: %%s", dwError);
  147. }
  148.  
  149. __int64 File64::_posFile() {
  150.     return i64FilePosition;
  151. }
  152.  
  153. __int64 File64::_sizeFile() {
  154.     DWORD dwLow, dwHigh;
  155.     DWORD dwError;
  156.  
  157.     dwLow = GetFileSize(hFile, &dwHigh);
  158.  
  159.     if (dwLow == 0xFFFFFFFF && (dwError = GetLastError()) != NO_ERROR)
  160.         throw MyWin32Error("Cannot determine file size: %%s", dwError);
  161.  
  162.     return ((__int64)dwHigh << 32) | (unsigned long)dwLow;
  163. }
  164.